package com.webcash.CNBoard.Service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webcash.CNBoard.Model.Employee;
import com.webcash.CNBoard.Model.Schedule;
import com.webcash.CNBoard.Model.ScheduleMonthly;
import com.webcash.CNBoard.Model.Team;
import com.webcash.CNBoard.Repository.EmployeeRepository;
import com.webcash.CNBoard.Repository.ScheduleRepository;

@Service
public class EmployeeServiceImpl implements EmployeeService {

	@Autowired
	EmployeeRepository employeeRepository;
	@Autowired
	ScheduleRepository scheduleRepository;

	private static int FAIL_NOT_USER = 0;
	private static int FAIL_NOT_MATCHED_PW = 1;
	private static int SUCCESS = 3;

	public int checkValidation(String id, String password) {

		Employee emp = employeeRepository.selectEmployeeById(id);
		if (emp == null)
			return FAIL_NOT_USER;

		emp = employeeRepository.isValidPassword(id, password);

		if (emp == null)
			return FAIL_NOT_MATCHED_PW;

		return SUCCESS;

	}

	public Employee getEmployeeById(String id) {
		return employeeRepository.selectEmployeeById(id);
	}

	public List<Employee> getDeptEmployeeList(int deptNo) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void getUpdateAccountResult(Employee emp){
		 employeeRepository.updateEmployeeAccountbyEmp(emp);
	}
	
	public void setUpdateAccountImgPath(Employee emp){
		employeeRepository.updateEmployeeAccountImgPathbyEmp(emp);
	}

	public List<Employee> getTeamMemberListbydeptNoAndTeamNo(Employee emp) {
		return employeeRepository.selectTeamMemberListbyDeptNoAndTeamNo(emp);
	}

	public List<Employee> getTeamMemberListbydeptNoAndTeamNo(Team team) {
		return employeeRepository.selectTeamMemberListbyDeptNoAndTeamNo(team);
	}

	public Employee getEmployeebyEmpNo(int empNo) {
		return employeeRepository.selectEmployeeByEmpNo(empNo);
	}

	public List<Schedule> getScheduleListByEmpNo(int empNo) {
		return scheduleRepository.selectScheduleListByEmpNo(empNo);
	}

	public void addScheduleByEmpNoAndSchedule(int empNo, Schedule schedule) {
		
		if (schedule.getScheduleStartDateString() == null
				|| schedule.getScheduleStartDateString().equals("")) {
			Date date = new Date(0,0,0);
			date.setHours(schedule.getStartTime());
			schedule.setScheduleStartDate(date);
		}else{
			String value = schedule.getScheduleStartDateString();
			String[] yyyymmdd = value.split("-");
			System.out.println(Integer.parseInt(yyyymmdd[1]));
			Date date = new Date();
			date.setYear(Integer.parseInt(yyyymmdd[0]) - 1900);
			date.setMonth(Integer.parseInt(yyyymmdd[1]) - 1);
			date.setDate(Integer.parseInt(yyyymmdd[2]));
			date.setHours(schedule.getStartTime());
			schedule.setScheduleStartDate(date);
		}

		if (schedule.getScheduleEndDateString() == null
				|| schedule.getScheduleEndDateString().equals("")) {
			Date date = new Date(0,0,0);
			date.setHours(schedule.getEndTime());
			schedule.setScheduleEndDate(date);
		}else{
			String value = schedule.getScheduleEndDateString();
			String[] yyyymmdd = value.split("-");
			Date date = new Date();
			date.setYear(Integer.parseInt(yyyymmdd[0]) - 1900);
			date.setMonth(Integer.parseInt(yyyymmdd[1]) - 1);
			date.setDate(Integer.parseInt(yyyymmdd[2]));
			date.setHours(schedule.getEndTime());
			schedule.setScheduleEndDate(date);
		}
		scheduleRepository.insertScheduleByEmpNoAndSchedule(empNo, schedule);
	}

	public void removeScheduleByScheduleNo(int scheduleNo) {
		scheduleRepository.deleteScheduleByScheduleNo(scheduleNo);
	}

	public void insertEmployeebyEmp(Employee emp) {
		employeeRepository.insertEmployeeByEmp(emp);
		
	}

	public List<Employee> getAllEmployeeList() {
		return employeeRepository.selectAllEmployee();
	}

	public Team getMyTeamInfoByTeamNoAndDeptNo(String teamNo, String deptNo) {
		return employeeRepository.selectMyTeamInfoByTeamNoAndDeptNo(teamNo, deptNo);
	}

	public List<ScheduleMonthly> getScheduleMonthlyListByEmpNo(int empNo) {
		return scheduleRepository.selectScheduleMonthlyListByEmpNo(empNo);
	}

	public void addScheduleMonthlyByEmpNoAndScheduleMonthly(int empNo,
			ScheduleMonthly scheduleMonthly) {
		
		
		if (scheduleMonthly.getScheduleStartDateString() == null
				|| scheduleMonthly.getScheduleStartDateString().equals("")) {
			Date date = new Date(0,0,0);
			date.setHours(scheduleMonthly.getStartTime());
			scheduleMonthly.setStartDate(date);
		}else{
			String value = scheduleMonthly.getScheduleStartDateString();
			String[] yyyymmdd = value.split("-");
			System.out.println(Integer.parseInt(yyyymmdd[1]));
			Date date = new Date();
			date.setYear(Integer.parseInt(yyyymmdd[0]) - 1900);
			date.setMonth(Integer.parseInt(yyyymmdd[1]) - 1);
			date.setDate(Integer.parseInt(yyyymmdd[2]));
			date.setHours(scheduleMonthly.getStartTime());
			scheduleMonthly.setStartDate(date);
		}

		if (scheduleMonthly.getScheduleEndDateString() == null
				|| scheduleMonthly.getScheduleEndDateString().equals("")) {
			Date date = new Date(0,0,0);
			date.setHours(scheduleMonthly.getEndTime());
			scheduleMonthly.setEndDate(date);
		}else{
			String value = scheduleMonthly.getScheduleEndDateString();
			String[] yyyymmdd = value.split("-");
			Date date = new Date();
			date.setYear(Integer.parseInt(yyyymmdd[0]) - 1900);
			date.setMonth(Integer.parseInt(yyyymmdd[1]) - 1);
			date.setDate(Integer.parseInt(yyyymmdd[2]));
			date.setHours(scheduleMonthly.getEndTime());
			scheduleMonthly.setEndDate(date);
		}
		scheduleRepository.insertScheduleMonthlyByEmpNoAndScheduleMonthly(empNo, scheduleMonthly);
	}
	
	
	
}
